<template>
  <div class="login">
    <div class="main">
      <h2>欢迎登录</h2>
      <div class="line"></div>
      <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules"
        label-width="auto" class="demo-ruleForm">
        <el-form-item label="" prop="checkUname" class="username">
          <el-input v-model="ruleForm.checkUname" type="username" autocomplete="off"  :prefix-icon="User" />
        </el-form-item>
        <el-form-item label="" prop="checkPass" class="password">
          <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" :prefix-icon="Unlock" />
        </el-form-item>
        <el-form-item class="button">
          <el-button @click="submitForm(ruleFormRef)"
            style="width: 240px;background-color: dodgerblue;color: white;">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { Unlock, User } from '@element-plus/icons-vue';
import router from '@/router';

const ruleFormRef = ref<FormInstance>()


const validateUname = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else if(value.length<3||value.length>6){
    callback(new Error('用户名长度为3-6个字符'))
  } else{
    callback()
  }
}
const validatePass = (rule: any, value: any, callback: any) => {
  var passwordreg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{6,18}$/;
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if(!passwordreg.test(value)) {
    callback(new Error('密码必须至少包括大写字母、小写字母、数字、特殊符号中的一个组成!'))
  }else if(value.length<6||value.length>18){
    callback(new Error('用户名长度为6-18个字符'))
  }else{
    callback()
  }
}



const ruleForm = reactive({
  checkUname: '',
  checkPass: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
  checkUname: [{ required:true,validator: validateUname, trigger: 'blur' }],
  checkPass: [{ required:true ,validator: validatePass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
      router.push("/")
    } else {
      console.log('error submit!')
      return false
    }
  })
}

</script>

<style lang="less" scoped>
.login {
  background-color: dodgerblue;
  height: 600px;
  width: 100%;

  .main {
    width: 280px;
    height: 280px;
    background-color: white;
    float: right;
    margin-top: 200px;
    margin-right: 180px;
    border: 6px solid white;
    border-radius: 5px;

    h2 {
      margin-left: 90px;
      margin-top: 30px;
    }

    .line {
      margin-left: 90px;
      width: 90px;
      height: 4px;
      background-color: dodgerblue;
    }
    .username{
      margin-top: 20px;
      margin-left: 4px;
    }
    .password{
      margin-top: 20px;
      margin-left: 4px;
    }
       .button {
      margin-left: 14px;
      margin-top: 30px;
    }
   
  }
}
</style>